package cn.knet.excel.imp.knetexceltool.controller;

import cn.knet.excel.imp.knetexceltool.DTO.KnetBgdataCompanyBase;
import cn.knet.excel.imp.knetexceltool.DTO.KnetBgdataKeyword;
import cn.knet.excel.imp.knetexceltool.service.KnetBgdataCompanyBaseService;
import cn.knet.excel.imp.knetexceltool.task.KnetBgdataImportTask;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.atomic.LongAdder;

@Controller
@Slf4j
public class KnetBgdataCompanyController {

    @Resource
    private KnetBgdataCompanyBaseService knetBgdataCompanyBaseService;

    @Value("${excel.dir}")
    private String srcFile;

    @RequestMapping(value = "/bgdata/imp/start", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String start() throws Exception {
        long stime = System.currentTimeMillis();
//        KnetBgdataCompanyBase knetBgdataCompanyBase = new KnetBgdataCompanyBase();
//        knetBgdataCompanyBase.setOrgName("hxb");
//        knetBgdataCompanyBaseService.insert(knetBgdataCompanyBase);

        LongAdder okAdder = new LongAdder();
        LongAdder failAdder = new LongAdder();
        KnetBgdataImportTask knetBgdataTask = new KnetBgdataImportTask();
        knetBgdataTask.readDir(srcFile, KnetBgdataCompanyBase.class, okAdder,failAdder);
        return "导入成功";
    }

    /**
     * 根据输入的企业名称，进行过滤、拆分
     * @param corpname
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/bigdata/clean", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String cleanStart(String corpname,String corptype) throws Exception {
        KnetBgdataKeyword knetBgdataKeyword= knetBgdataCompanyBaseService.cleanRules(corpname,corptype);
        return JSONObject.toJSONString(knetBgdataKeyword);
    }

}
